package contrl

import (
	"core/mysql"
	"core/rds"
	"core/utils"
	"db"
	"fmt"
	"github.com/gin-gonic/gin"
)

type AdminCtl struct{}

func (*AdminCtl) GetUser(c *gin.Context) (res interface{}, e Error) {
	u, err := db.LoadByUID(GetInt64(c, "uid"), true)
	if err != nil {
		return nil, GenError(err)
	}

	res = map[string]interface{}{
		"User": *u,
	}
	return
}

func (*AdminCtl) DelUser(c *gin.Context) (res interface{}, e Error) {
	u, err := db.LoadByUID(GetInt64(c, "uid"), true)
	if err != nil {
		return nil, GenError(err)
	}
	err = db.DelOpenIdData(u.OpenID)
	if err != nil {
		return nil, GenError(err)
	}
	res = "ok"
	return
}

func (*AdminCtl) DelCacheUser(c *gin.Context) (res interface{}, e Error) {
	u, err := db.LoadByUID(GetInt64(c, "uid"), true)
	if err != nil {
		return nil, GenError(err)
	}

	err = rds.GetCacheClient(u.UID).Del(db.MakeUserKey(u.UID)).Err()
	err = rds.GetCacheClient(u.UID).Del(db.MakeUserLevelKey(u.UID)).Err()
	if err != nil {
		return nil, GenError(err)
	}
	res = "ok"
	return
}

func (*AdminCtl) AddActiveUser(c *gin.Context) (res interface{}, e Error) {
	var uid int64
	uids := make([]int64, 0)
	for uid = 0; uid < 800; uid++ {
		u, _ := db.LoadByUID(uid, true)
		if u == nil || u.UID == 0 {
			continue
		}
		err := db.AddActiveUser(uid)
		if err != nil {
			return nil, GenError(err)
		}
		uids = append(uids, uid)
	}
	res = uids
	return
}

func (*AdminCtl) SetVersion(c *gin.Context) (res interface{}, e Error) {
	devVersion := c.GetString("devVersion")
	auditVersion := c.GetString("auditVersion")
	if devVersion == "" || auditVersion == "" {
		return nil, GenErrorMsg("version不能为空")
	}
	err := db.SaveVersion(devVersion, auditVersion)
	if err != nil {
		return nil, GenError(err)
	}
	res = "ok"
	return
}

func (*AdminCtl) GetVersion(c *gin.Context) (res interface{}, e Error) {
	devVersion, auditVersion := db.LoadVersion()
	res = map[string]interface{}{
		"devVersion":   devVersion,
		"auditVersion": auditVersion,
	}
	return
}

func (*AdminCtl) AddGift(c *gin.Context) (res interface{}, e Error) {
	key := c.GetString("key")
	val := GetInt(c, "val")
	u, err := db.LoadByUID(GetInt64(c, "uid"), true)
	if err != nil {
		return nil, GenError(err)
	}
	saveFields := make([]string, 0)
	switch key {
	case db.FieldGrass:
		u.Grass += val
		saveFields = append(saveFields, db.FieldGrass)
	}
	err = u.Save(saveFields...)
	if err != nil {
		return nil, GenError(err)
	}
	res = u
	return
}

func (*AdminCtl) MoveOpenId(c *gin.Context) (res interface{}, e Error) {
	start := GetInt64(c, "start")
	end := GetInt64(c, "end")
	go moveOpenIds(start, end)
	res = "running"
	return
}

func moveOpenIds(start, end int64) {
	defer utils.CatchPanic()
	var success int
	type failedType struct {
		Uid int64
		Err error
	}
	failedInfo := make([]failedType, 0)
	for uid := start; uid < end; uid++ {
		err := moveOpenId(uid)
		if err != nil {
			utils.Error(fmt.Sprintf("移动%d出错,err:%v", uid, err))
			failedInfo = append(failedInfo, failedType{Uid: uid, Err: err})
		} else {
			success++
			fmt.Printf("%d, ", uid)
		}
	}
	utils.Info()
	utils.Info("成功：", success)
	utils.Info("失败：", failedInfo)
}

func moveOpenId(uid int64) (err error) {
	user, err := db.LoadFromMysql(uid)
	if err == mysql.NotFound {
		utils.Info("not found", uid)
		return nil
	} else if err != nil {
		return
	}
	return db.CreateOpenIdData(user.OpenID, user.UID)
}

func (*AdminCtl) Health() (res interface{}, e Error) {
	return "ok", Error{}
}
